<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>ERP管理系统</title>
    <th:block th:include="include::header"/>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">

            <input type="hidden" id="id" name="id" th:value="${upsubmenu.id}">
            <input type="hidden" id="rename" name="rename" th:value="${upsubmenu.parent}">

            <div class="layui-form-item">
                <label for="parent" class="layui-form-label">
                    <span class="x-red">*</span>上级菜单
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="parent" name="parent" required="" lay-verify="required"
                           autocomplete="off" class="layui-input" th:value="${upsubmenu.parent}">
                </div>
            </div>


            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>菜单名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" required="" lay-verify="required"
                           autocomplete="off" class="layui-input" th:value="${upsubmenu.name}">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="url" class="layui-form-label">
                    <span class="x-red">*</span>请求路径
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="url" name="url" required="" lay-verify="required"
                           autocomplete="off" class="layui-input" th:value="${upsubmenu.url}">
                </div>
            </div>

            <div class="layui-form-item" th:each="item:${subpower}">
                <label for="url" class="layui-form-label">
                    权限名
                </label>
                <div class="layui-input-inline">
                    <input type="text" power="power" required=""
                           autocomplete="off" th:pid="${item.powerId}" class="layui-input" th:value="${item.name}">
                </div>
            </div>

            <button id="addpower" type="button" class="layui-btn layui-btn-normal layui-btn-sm">添加权限</button>
            <div id="inputs"></div>


            <br><br>
            <div class="layui-form-item">
                <button  class="layui-btn" lay-filter="update" lay-submit="">
                    保存
                </button>
            </div>
        </form>
    </div>
</div>
<script >layui.use(['form', 'layer'],
    function() {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        $(".select").css("width","190px");


        //监听提交
        var upath=ctx+'/menu/update';
        form.on('submit(update)',
            function(data) {
                var powers=$("input[power='power']");
                var ins=[];
                var upids=[];
                var dpids=[];
                var newins=[];
                if(powers.length!=0){
                    for (var i=0;i<powers.length;i++){
                        if(powers[i].value!=null&&powers[i].value!=""&&powers[i].getAttribute("pid")!=null){
                            ins.push(powers[i].value);
                            upids.push(powers[i].getAttribute("pid"));
                        }else if(powers[i].value==null||powers[i].value==""&&powers[i].getAttribute("pid")!=null){
                            dpids.push(powers[i].getAttribute("pid"));
                        }else if(powers[i].value!=null&&powers[i].value!=""&&powers[i].getAttribute("pid")==null){
                            newins.push(powers[i].value);
                        }
                    }
                }
                var ins=JSON.stringify(ins);
                var upids=JSON.stringify(upids);
                var dpids=JSON.stringify(dpids);
                var newins=JSON.stringify(newins);
                data.field['ins']=ins;
                data.field['upids']=upids;
                data.field['dpids']=dpids;
                data.field['newins']=newins;
                $.post(upath,data.field,function(date){
                    if(date.success){
                        layer.alert(date.msg, {
                                icon: 6
                            },
                            function() {
                                //关闭当前frame
                                xadmin.close();

                                // 可以对父窗口进行刷新
                                // xadmin.father_reload();
                                window.parent.parent.location.reload();
                            });
                    }else{
                        layer.alert(date.msg, {icon:5});
                        return;
                    }
                });
                return false;
            });

        var but=$("#addpower");
        but.click(function (){
            var ints=$("input[power='power']");
            if(ints.length>5){
                layer.msg("最多添加6个权限", {icon:5});
                return;
            }
            var inputs=$("#inputs");
            html="<div class=\"layui-form-item\">\n" +
                "                <label class=\"layui-form-label\">\n" +
                "                    权限名\n" +
                "                </label>\n" +
                "                <div class=\"layui-input-inline\">\n" +
                "                    <input type=\"text\"  required=\"\" \n" +
                "                           autocomplete=\"off\" class=\"layui-input\" power='power'>\n" +
                "                </div>\n" +
                "            </div>";
            inputs.before(html);
        })

    });</script>
</body>

</html>
